package io.renren.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.dao.DemandManagementDao;
import io.renren.dao.InterviewDao;
import io.renren.dao.RecruitdamandDao;
import io.renren.entity.DemandManagementEntity;
import io.renren.entity.InterviewEntity;
import io.renren.service.DemandManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("demandManagementService")
public class DemandManagementServiceImpl implements DemandManagementService {
	@Autowired
	private DemandManagementDao demandManagementDao;
	@Autowired
	private RecruitdamandDao recruitdamandDao;
	@Autowired
	private InterviewDao interviewDao;

	@Override
	public DemandManagementEntity queryObject(Integer id){
		return demandManagementDao.queryObject(id);
	}
	
	@Override
	public List<DemandManagementEntity> queryList(Map<String, Object> map){
		return demandManagementDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return demandManagementDao.queryTotal(map);
	}
	
	@Override
	public void save(DemandManagementEntity demandManagement){
		demandManagementDao.save(demandManagement);
	}

	@Override
	public void update(DemandManagementEntity demandManagement){
		demandManagementDao.update(demandManagement);
	}
	
	@Override
	public void delete(Integer id){
		demandManagementDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		demandManagementDao.deleteBatch(ids);
	}


	@Override
	public void approve(DemandManagementEntity pojo,String username){
		saveApproveInfo(pojo,"同意",username);

	}

	@Override
	public void reject(DemandManagementEntity pojo,String username){
		saveApproveInfo(pojo,"拒绝",username);

	}



	public void saveApproveInfo(DemandManagementEntity pojo,String result,String username){
		String approveType=pojo.getApproveType();

		if("fir".equals(approveType)) {
			pojo.setFirApprover(username);
			pojo.setFirApprovalDate(new Date());
			pojo.setFirApprovalNote(pojo.getApproverNote());
			pojo.setResult("一级审核"+result);
			pojo.setFirApprovalResult(result);
		}else
			if("sec".equals(approveType)) {
			pojo.setSecApprover(username);
			pojo.setSecApprovalDate(new Date());
			pojo.setSecApprovalNote(pojo.getApproverNote());
			pojo.setResult("二级审核"+result);
			pojo.setSecApprovalResult(result);
		}else
			if("thi".equals(approveType)) {
				pojo.setThiApprover(username);
				pojo.setThiApprovalDate(new Date());
				pojo.setThiApprovalNote(pojo.getApproverNote());
				pojo.setResult("三级审核"+result);
				pojo.setThiApprovalResult(result);
		}else{
				pojo.setFouApprover(username);
				pojo.setFouApprovalDate(new Date());
				pojo.setFouApprovalNote(pojo.getApproverNote());
				pojo.setResult("四级审核"+result);
				pojo.setFouApprovalResult(result);
			}

		demandManagementDao.update(pojo);
	}




	@Override
	public List<Map<String, Object>> choosePerson(JSONArray ids) {
		List<Map<String, Object>> res = new ArrayList<>();
		for (Object id : ids) {
			Map<String, Object>  map = new HashMap<String, Object>();
			DemandManagementEntity entity = queryObject(Integer.valueOf(id.toString()));
			int personnum =0;
			if(entity.getDemandNumber()!=null){
				personnum=entity.getDemandNumber();
			}
			map.put("companyId",entity.getSettlement());
			map.put("company",entity.getSettlementName());

			map.put("job",entity.getDemandPost());
			map.put("sex",entity.getSex());
			map.put("topage",entity.getMaxAge());
			map.put("lowage",entity.getMinAge());
			map.put("papername","");
			List<Map<String, Object>> result = recruitdamandDao.choosePerson(map);
			if(result.size()<personnum*2){
				map.remove("papername");
				result = recruitdamandDao.choosePerson(map);
				if(result.size()<personnum*2){
					map.remove("lowage");
					result = recruitdamandDao.choosePerson(map);
					if(result.size()<personnum*2){
						map.remove("sex");
						result = recruitdamandDao.choosePerson(map);
					}
				}
			}
			res.addAll(result);
			for (Map<String, Object> re : res) {
				re.put("damandid",entity.getId());//需求编号
				String settlement=entity.getSettlement();
				re.put("settlementno",settlement);
				re.put("settlementid",settlement);
				re.put("settlementname",entity.getSettlementName());

				/*if(StringUtils.isNotBlank(settlement)){
					Map mps=new HashMap();
					mps.put("value",settlement);
					List<Map<String, Object>> list=demandManagementDao.queryDictList(settlement);
					if(list!=null&&list.size()>0){
						re.put("settlementname",list.get(0).get("label"));
					}
				}*/

				re.put("jobid",entity.getDemandPost());
				re.put("jobname",entity.getDemandPost());
			}
		}
		return res;
	}

	@Override
	@Transactional
	public int selectedEmp(JSONArray jsonArray) {
		int res = 0;
		try{
			for(int i = 0; i<jsonArray.size(); i++){
				JSONObject json = jsonArray.getJSONObject(i);
				InterviewEntity inter = new InterviewEntity();
			//	inter.setRecruitdamandid(json.getString("id"));
				inter.setDamandid(json.getString("damandid"));
				inter.setAge(json.getInteger("age"));
				inter.setEducation(json.getString("education"));
				inter.setJobid(json.getString("jobid"));
				inter.setJobname(json.getString("jobname"));
				inter.setMarried(json.getString("married"));
				inter.setEmpname(json.getString("name"));
				inter.setEmpid(json.getInteger("id"));
				inter.setCertificate(json.getString("certificate"));
				inter.setSex(json.getString("sex"));
				inter.setCompany(json.getString("settlementname"));
				inter.setCompanyid(json.getString("settlementid"));
				inter.setCompanyid(json.getString("settlementno"));

				inter.setFirInterviewStatus("0");

				interviewDao.save(inter);
				recruitdamandDao.changeFreeFlag(json.getInteger("id"));
				res = 1;
			}
		}catch (Exception e){
			res = 0;
			e.printStackTrace();
		}
		return res;
	}



}